package cn.wizzer.app.web.commons.ext.websocket.action.msg.attendant;

import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.models.vo.UserOnlineVo;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.filter.AttendantFilter;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zl
 * @date 2020/7/21 09:42
 */
@IocBean
@NettyAction(uri = "/attendant/online", filters = {TokenFilter.class, AttendantFilter.class})
public class AttendantOnlineAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserService userService;

    /**
     * 获取当前在线客服列表
     *
     * @param income
     * @param params
     * @return
     */
    @Override
    public Result action(Channel income, JSONObject params) {
        //查询不包括自己的当前在线客服
        List<User> users = userService.query(Cnd.where("is_online", "=", 1).and("attendant", "=", 1).and("id", "!=", params.getString("appid")));
        //过滤未进入消息中的客服
        List<UserOnlineVo> userOnlineVoList = users.stream()
                .map(user -> JSONObject.parseObject(JSONObject.toJSONString(NettyCache.globalGet(user.getId())), UserOnlineVo.class))
                .filter(Objects::nonNull).collect(Collectors.toList());
        return Result.success().addData(userOnlineVoList);
    }
}
